import loopSpriteAssembler from "./loopSpriteAssembler";

const { ccclass, property, executeInEditMode } = cc._decorator;
const gfx = cc['gfx'];
@ccclass
@executeInEditMode
export default class loopSprite extends cc.Sprite {

    _assembler: loopSpriteAssembler = null;
    meshRenderer: cc.MeshRenderer = null;

    _moveSpeed: cc.Vec2 = cc.v2(-0.1, 0);
    get moveSpeed() {
        return this._moveSpeed;
    }
    set moveSpeed(v: cc.Vec2) {
        this._moveSpeed = v;
        this.updateVertexes();
    }

    updateVertexes() {
        console.log('updateVertexes')
        //@ts-ignore
        let assembler: loopSpriteAssembler = this._assembler;
        if (!assembler) {
            return;
        }

        assembler.moveSpeed = this.moveSpeed;
        // assembler.genVert();
        this.setVertsDirty();
    }

    onEnable() {
        super.onEnable();
    }

    _resetAssembler() {
        // this.moveSpeed = cc.v2(0.1, 0.1);
        this.setVertsDirty();
        let assembler = this._assembler = new loopSpriteAssembler();
        this.updateVertexes();

        assembler.init(this);

        //@ts-ignore
        this._updateColor();
    }

    startLoop() {
        this.isLoop = true;
    }
    stopLoop() {
        this.isLoop = false;
    }

    // 传入时间
    isLoop: boolean = true;
    dt: number = 0;
    update(dt) {
        if (this.isLoop) {
            this.dt += dt;
            let sp = this.node.getComponent(cc.Sprite)
            let mat = sp.getMaterial(0);
            if (mat && mat.name.match(/loopSprite/)) {
                mat.setProperty('time', this.dt);
                // mat.setProperty('moveSpeed', this.moveSpeed);
                sp.setMaterial(0, mat);
            }
        }
    }
}
